/*----------- Projectile -----------*/
var PROJECTILE_MOVE_UP = 0;
var PROJECTILE_MOVE_RIGHT = 1;
var PROJECTILE_MOVE_DOWN = 2;
var PROJECTILE_MOVE_LEFT = 3;
var PROJECTILE_EXPLODE = 4;

function Projectile(owner, x, y, collisionManager) {
    this.collisionManager = collisionManager;
    this.x = x;
    this.y = y;
    this.halfWidth = 2;
    this.halfHeight = 2;
    this.x1 = this.x - this.halfWidth;
    this.x2 = this.x + this.halfWidth;
    this.y1 = this.y - this.halfHeight
    this.y2 = this.y + this.halfHeight
    this.dx = 0;
    this.dy = 0;
    this.remove = false;
    this.visible = true;
    this.owner = owner;

    var pUp = new ImageSprite(IMAGE, {
        cellWidth: 32,
        cellHeight: 32,
        gridSpace: 1,
        x: 4,
        y: 1,
        offsetX: 16,
        offsetY: 16
    });

    var pRight = new ImageSprite(IMAGE, {
        cellWidth: 32,
        cellHeight: 32,
        gridSpace: 1,
        x: 6,
        y: 1,
        offsetX: 16,
        offsetY: 16
    });

    var pDown = new ImageSprite(IMAGE, {
        cellWidth: 32,
        cellHeight: 32,
        gridSpace: 1,
        x: 7,
        y: 1,
        offsetX: 16,
        offsetY: 16
    });

    var pLeft = new ImageSprite(IMAGE, {
        cellWidth: 32,
        cellHeight: 32,
        gridSpace: 1,
        x: 5,
        y: 1,
        offsetX: 16,
        offsetY: 16
    });

    var explosion = new AnimatedSprite(IMAGE, {
        cellWidth: 32,
        cellHeight: 32,
        interval: 150,
        offsetX: 16,
        offsetY: 16,
        gridSpace: 1,
        frames: [{
            x: 1,
            y: 1
        }, {
            x: 2,
            y: 1
        }, {
            x: 3,
            y: 1
        }]
    });

    this.sprites = [pUp, pRight, pDown, pLeft, explosion];
    this.spriteIndex = PROJECTILE_MOVE_UP;
    this.collisionManager.objects.push(this);
}

Projectile.prototype.update = function() {
    this.x += this.dx;
    this.y += this.dy;
    this.x1 = this.x - this.halfWidth;
    this.x2 = this.x + this.halfWidth;
    this.y1 = this.y - this.halfHeight
    this.y2 = this.y + this.halfHeight
};

Projectile.prototype.render = function(context) {
    if(this.spriteIndex == PROJECTILE_EXPLODE){
        this.sprites[this.spriteIndex].playOnce();
    }
    
    if (this.visible) {
        this.sprites[this.spriteIndex].x = this.x;
        this.sprites[this.spriteIndex].y = this.y;
        this.sprites[this.spriteIndex].render(context);
    }
};

Projectile.prototype.onCollide = function(obj) {
    if (obj == null){
        this.explode();
    }
    if(obj instanceof Tank && obj !== this.owner) {
        this.explode();
    }
    if(obj instanceof Block){
        this.explode();
    }
};

Projectile.prototype.explode = function(){
    this.spriteIndex = PROJECTILE_EXPLODE;
    this.dx = 0;
    this.dy = 0;
    this.remove = true;
    var self = this;
    this.sprites[this.spriteIndex].onPlayEnd = function() {
        self.visible = false;
    };  
};
/*----------- end Projectile -----------*/